<?php

App::uses('AppModel', 'Model');

/**
 * Banner Model
 *
 * @property Midia $Midia
 * @property Usuario $Usuario
 * @property BannersPosicao $BannersPosicao
 */
class Banner extends BannersAppModel {

    public $table = 'banners';
    public $name = 'Banner';
    public $actsAs = array('CakePtbr.AjusteData', 'CakePtbr.AjusteFloat', 'Search.Searchable');
    //`id`, `nome`, `status`, `midia_id`, `created`, `modified`, `updated`, `usuario_id`, `url`, `banners_posicao_id`
    public $filterArgs = array(
        'id' => array('type' => 'value'),
        'nome' => array('type' => 'like'),
        'status' => array('type' => 'value'),
        'ordem' => array('type' => 'value'),
        'midia_id' => array('type' => 'value'),
        'created' => array('type' => 'like'),
        'modified' => array('type' => 'like'),
        'updated' => array('type' => 'like'),
        'usuario_id' => array('type' => 'value'),
        'url' => array('type' => 'like'),
        'exibir_no_conteudo' => array('type' => 'like'),
        'banners_posicao_id' => array('type' => 'value'),
    );

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'nome' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                'required' => TRUE,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'ordem' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                'required' => TRUE,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'status' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                'required' => TRUE,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'midia_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                'required' => TRUE,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'banners_posicao_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                'required' => TRUE,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Midia' => array(
            'className' => 'Midia',
            'foreignKey' => 'midia_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'BannersPosicao' => array(
            'className' => 'BannersPosicao',
            'foreignKey' => 'banners_posicao_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function afterFind($results, $primary = false) {

        $bannerHaspaginas = ClassRegistry::init('Banners.BannersHasPagina');
        $paginas = ClassRegistry::init('Paginas.Pagina');
        $bannerHaspaginas->recursive = $paginas->recursive = -1;

        foreach ($results as $key => $value) {
            $resultsHas = $bannerHaspaginas->findAllByBannersId($results[0]['Banner']['id']);
            if (count($resultsHas) > 0) {
                foreach ($resultsHas as $k => $v) {
                    $resultsPagina = $paginas->findById($v['BannersHasPagina']['paginas_id']);
                    if (count($resultsPagina) > 0) {
                        $results[$key]['BannersHasPagina'][$k] = $v['BannersHasPagina'];
                        $results[$key]['BannersHasPagina'][$k]['Pagina'] = $resultsPagina['Pagina'];
                    }
                }
            }
        }
        return parent::afterFind($results, $primary);
    }

}
